import psutil

pids = psutil.pids()

for pid in pids:
    print(pid)


pid = 75120     # prl_updater_ctl
proc_name = 'prl_updater_ctl'


def search_pid_by_name(proc_name):
    pid = -1
    for proc in psutil.process_iter():
        try:
            if proc.name() == proc_name:
                pid = proc.pid
                print("pid-%d,name:%s" % (proc.pid,proc.name()))
                break
        except Exception as e:
            pass
    return pid

pid = search_pid_by_name(proc_name)


search_pid_by_name('hahhaa')


from bdtime import tt


def get_pid_info(pid, inter):
    p = psutil.Process(pid)

    # 休眠一会儿后的第二次才能读对
    for i in range(2):
        tt.sleep(0.01)
        cpu_percent = p.cpu_percent()
    mem_percent = p.memory_percent()
    print("cpu:{:.2f}%,mem:{:.2f}%".format(cpu_percent,mem_percent))
    ret = 1
    return ret


def get_cpu_percent(pid):
    p = psutil.Process(pid)

    # 休眠一会儿后的第二次才能读对
    for i in range(2):
        tt.sleep(0.01)
        cpu_percent = p.cpu_percent()
    return cpu_percent

get_cpu_percent(pid)


get_pid_info(pid)


import psutil
psutil.cpu_percent()
# psutil.cpu_stats().
mem = psutil.virtual_memory()
# 系统总计内存
zj = float(mem.total) / 1024 / 1024 / 1024
# 系统已经使用内存
ysy = float(mem.used) / 1024 / 1024 / 1024
# 系统空闲内存
kx = float(mem.free) / 1024 / 1024 / 1024
print('系统总计内存:%dGB' % zj)
print('系统已经使用内存:%dGB' % ysy)
print('系统空闲内存:%dGB' % kx)
